<?php
class Util_Track{
    public static function getTrackList($limit,$offset,$uid){
    	
        list($counts,$data) = self::getTrack($limit,$offset,$uid);
        $list = array();
        foreach($data as $v){
            $tmp = User_TouristComments::tourist_track_check_out('track_id',$v['id']);
            //行程评分
            $tavg = User_TouristComments::tourist_track_score('track_id',$v['id']);
            //玩伴评分
            $gavg =User_TouristComments::tourist_guider_score('track_id', $v['id']);
            $avg=($tavg+$gavg)/2;
            $count = $tmp->count();

            $v['avg'] = sprintf("%.2f",$avg);
            $v['count'] = $count;
            $v['created'] = date('Y.m.d H:i',$v['created']);
            $list[] = $v;
        }
        return array($counts,$list);
    }

    public static function getTrackDetail($uid,$id,$type){
    	switch ($type)
    	{
    		case 'TRACK':
    			$table='mod_track';
    			break;
    		case 'ACTIVITY':
    			$table='mod_activity';
    			break;
    		case 'ZIYING':
    			$table='mod_ziying';
    			break;
    		default:
    			$table=NULL;
    			break;
    	}
        list($counts,$data) =self::getTrackOne(NULL,NULL,$uid,$id,$table);
        $list = array();
        foreach($data as $v){
            //行程评分
            $tavg = User_TouristComments::tourist_track_score('track_id',$v['id']);
            //玩伴评分
            $gavg =User_TouristComments::tourist_guider_score('track_id', $v['id']);
            $avg=($tavg+$gavg)/2;
            //取得评论数据
            $query = User_TouristComments::tourist_track_check_out('track_id',$v['id']);
            $count = $query->count();
            $comments = $query->as_array();
            foreach($comments as &$cv){
                $account = User_Account::account_checkout('uid',$cv['uid'])->current();
                $cv['userIcon'] = $account['userIcon'];
                $cv['nickname'] = $account['userName'];
                $cv['created'] = date("Y.m.d H:i:s",$cv['created']);
                $cv['tourist_pic'] = explode(';',trim($cv['tourist_pic'],';'));
            }
            //获得线路评分分布
            $track_sc3 = User_TouristComments::comments_check($v['id'],$type,array(
                'key'=>'track_score',
                'cond'=>'<=',
                'val'=>3)
            )->count();

            $track_sc5 = User_TouristComments::comments_check($v['id'],$type,array(
                'key'=>'track_score',
                'cond'=>'>=',
                'val'=>4)
            )->count();
            //获得玩伴评分分布
            $guider_sc3 = User_TouristComments::comments_check($v['id'],$type,array(
            		'key'=>'guider_score',
            		'cond'=>'<=',
            		'val'=>3)
            )->count();
            
            $guider_sc5 = User_TouristComments::comments_check($v['id'],$type,array(
            		'key'=>'guider_score',
            		'cond'=>'>=',
            		'val'=>4)
            )->count();

            $v['detail'] = $comments;
            $v['avg'] = sprintf("%.2f",$avg);
            $v['count'] = $count;
            $v['tracksc3'] = $track_sc3;
            $v['tracksc4'] = $count-$track_sc3-$track_sc5;
            $v['tracksc5'] = $track_sc5;
            $v['guidersc3'] = $guider_sc3;
            $v['guidersc4'] = $count-$guider_sc3-$guider_sc5;
            $v['guidersc5'] = $guider_sc5;
            $v['startDay'] = date("Y.m.d",strtotime($v['startDay']));
            $v['stopDay'] = date("Y.m.d",strtotime($v['stopDay']));
            $list = $v;
        }

        return $list;
    }
    //取一个类型的数据
    public static function getTrackOne($limit=null,$offset=null,$uid,$id = -1,$table=null){
    	if(empty($table)){
    		return array();
    	}
    	$query = DB::select('id','name','created','startDay','stopDay')
    	     ->from($table)
    	     ->where('status','=','VALID')
             ->where('uid','=',$uid);
        if($id!=-1){
            $query=$query->where('id','=',$id);
        }
        if(!empty($offset)){
        	$query=$query->offset($offset);
        }
        if(!empty($limit)){
        	$query=$query->limit($limit);
        }
        
        $counts = $query->execute()->count();
        $list = $query->execute()->as_array();
        return array($counts,$list);
             
    }
    //取活动路线自营的数据
    public static function getTrack($limit=null,$offset=null,$uid){
    	$count1 = DB::select('id')
    	->from('mod_track')
    	->where('status','=','VALID')
    	->where('uid','=',$uid);
    	$count2 = DB::select('id')
    	->union($count1)
    	->from('mod_activity')
    	->where('status','=','VALID')
    	->where('uid','=',$uid);
    	$count = DB::select('id')
    	->union($count2)
    	->from('mod_ziying')
    	->where('status','=','VALID')
    	->where('uid','=',$uid);
    	
    	$query1 = DB::select('id','name','created','startDay','stopDay')
            ->from('mod_track')
            ->where('status','=','VALID')
            ->where('uid','=',$uid)
            ->order_by('created','desc');
        
        if(!empty($offset)){
        	$query1 = $query1 ->offset($offset);
        }
        if(!empty($limit)){
        	$query1 = $query1->limit($limit);
        }
        
        $query2 = DB::select('id','name','created','startDay','stopDay')
            ->union($query1)
            ->from('mod_activity')
            ->where('status','=','VALID')
            ->where('uid','=',$uid);
                
        $query = DB::select('id','name','created','startDay','stopDay')
        ->union($query2)
        ->from('mod_ziying')
        ->where('status','=','VALID')
        ->where('uid','=',$uid);
           
        $counts = $count->execute()->count();
        $list = $query->execute()->as_array();
        return array($counts,$list);

    }
}